package com.scommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scommunity.admin.vo.UserQO;
import com.scommunity.domain.Emp;
import com.scommunity.domain.Owner;
import com.scommunity.domain.User;
import com.scommunity.mapper.UserMapper;
import com.scommunity.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    UserMapper userMapper;

    @CacheEvict(value = "users", allEntries = true)
    @Override
    public void addUser(User user) {
        userMapper.add(user);
    }

    @Override
    public PageInfo<User> findPageInfoByCondition(UserQO userQO, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.findPageInfoByCondition(userQO);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Cacheable(value = "users")
    @Override
    public List<User> getAll() {
        return userMapper.findAll();
    }

    @CacheEvict(value = "users", allEntries = true)
    @Override
    public void update(User user) {
        userMapper.update1(user);
    }

    @Override
    public void updateUserStatusById(String id, String status) {
        userMapper.updateUserStatusById(id, status);
    }

    @Override
    public User loginByAccount(String userName, String userPassword) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("user_name", userName).eq("user_password", userPassword));
    }

    @Override
    public User loginByPhone(String phone) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("user_name", phone));
    }

    @Override
    public Owner getInfoByUserId(Long userId) {
        return userMapper.getInfoByUserId(userId);
    }

    @Override
    public Long getOwnerId(Long userId) {
        return userMapper.getOwnerId(userId);
    }

}
